$(function(){
	//初始化
	var weightTypeValue = $("#updateWeightType").val();
	if(weightTypeValue == "" || weightTypeValue == "SR"){
		$("#updateWeightB")[0].style.display="none";
		$("#updateWeightText")[0].style.display="none";
		$("#updateWeightText").attr("readonly","readonly");
		$("#updateWeightBText")[0].style.display="none";
		$("#updateWeightEText")[0].style.display="none";
	}else if(weightTypeValue == "NOTRANGE"){
		$("#updateWeightB")[0].style.display="";
		$("#updateWeightText")[0].style.display="";
		$("#updateWeightBText")[0].style.display="none";
		$("#updateWeightEText")[0].style.display="none";
	}else if(weightTypeValue == "RANGE"){
		$("#updateWeightBText")[0].style.display="";
		$("#updateWeightEText")[0].style.display="";
		$("#updateWeightB")[0].style.display="none";
		$("#updateWeightText")[0].style.display="none";
		$("#updateWeightText").attr("readonly","readonly");
	}else{
		$("#updateWeightB")[0].style.display="none";
		$("#updateWeightText")[0].style.display="none";
		$("#updateWeightText").attr("readonly","readonly");
		$("#updateWeightBText")[0].style.display="none";
		$("#updateWeightEText")[0].style.display="none";
	}
	var AmountTypeValue = $("#updateAmountType").val();
	if(AmountTypeValue == "" || AmountTypeValue == "SR"){
		$("#updateAmountB")[0].style.display="none";
		$("#updateAmountText")[0].style.display="none";
		$("#updateAmountText").attr("readonly","readonly");
		$("#updateAmountBText")[0].style.display="none";
		$("#updateAmountEText")[0].style.display="none";
	}else if(AmountTypeValue == "NOTRANGE"){
		$("#updateAmountB")[0].style.display="";
		$("#updateAmountText")[0].style.display="";
		$("#updateAmountBText")[0].style.display="none";
		$("#updateAmountEText")[0].style.display="none";
	}else if(AmountTypeValue == "RANGE"){
		$("#updateAmountBText")[0].style.display="";
		$("#updateAmountEText")[0].style.display="";
		$("#updateAmountB")[0].style.display="none";
		$("#updateAmountText")[0].style.display="none";
		$("#updateAmountText").attr("readonly","readonly");
	}else{
		$("#updateAmountB")[0].style.display="none";
		$("#updateAmountText")[0].style.display="none";
		$("#updateAmountText").attr("readonly","readonly");
		$("#updateAmountBText")[0].style.display="none";
		$("#updateAmountEText")[0].style.display="none";
	}
	
	var buyCountTypeValue = $("#updateBuyCountType").val();
	if(buyCountTypeValue == "" || buyCountTypeValue == "SR"){
		$("#updateBuyCountB")[0].style.display="none";
		$("#updateBuyCountText")[0].style.display="none";
		$("#updateBuyCountText").attr("readonly","readonly");
		$("#updateBuyCountBText")[0].style.display="none";
		$("#updateBuyCountEText")[0].style.display="none";
	}else if(buyCountTypeValue == "NOTRANGE"){
		$("#updateBuyCountB")[0].style.display="";
		$("#updateBuyCountText")[0].style.display="";
		$("#updateBuyCountBText")[0].style.display="none";
		$("#updateBuyCountEText")[0].style.display="none";
	}else if(buyCountTypeValue == "RANGE"){
		$("#updateBuyCountBText")[0].style.display="";
		$("#updateBuyCountEText")[0].style.display="";
		$("#updateBuyCountB")[0].style.display="none";
		$("#updateBuyCountText")[0].style.display="none";
		$("#updateBuyCountText").attr("readonly","readonly");
	}else{
		$("#updateBuyCountB")[0].style.display="none";
		$("#updateBuyCountText")[0].style.display="none";
		$("#updateBuyCountText").attr("readonly","readonly");
		$("#updateBuyCountBText")[0].style.display="none";
		$("#updateBuyCountEText")[0].style.display="none";
	}
	
	//更新form
	$("select#updateWeightB").change(function(){
		var value = $("select#updateWeightB").val();
		if(value == ""){
			$("input#updateWeightText").val("");
			$("input#updateWeightText").attr("readonly","readonly");
		}else{
			$("input#updateWeightText").removeAttr("readonly");
		}
	});
	$("select#updateWeightE").change(function(){
		var value = $("select#updateWeightE").val();
		if(value == ""){
			$("#updateWeightEText").val("");
			$("#updateWeightEText").attr("readonly","readonly");
		}else{
			$("#updateWeightEText").removeAttr("readonly");
		}
	});
	$("select#updateAmountB").change(function(){
		var value = $("select#updateAmountB").val();
		if(value == ""){
			$("#updateAmountText").val("");
			$("#updateAmountText").attr("readonly","readonly");
		}else{
			$("#updateAmountText").removeAttr("readonly");
		}
	});
	$("select#updateAmountE").change(function(){
		var value = $("select#updateAmountE").val();
		if(value == ""){
			$("#updateAmountEText").val("");
			$("#updateAmountEText").attr("readonly","readonly");
		}else{
			$("#updateAmountEText").removeAttr("readonly");
		}
	});
	$("select#updateBuyCountB").change(function(){
		var value = $("select#updateBuyCountB").val();
		if(value == ""){
			$("#updateBuyCountText").val("");
			$("#updateBuyCountText").attr("readonly","readonly");
		}else{
			$("#updateBuyCountText").removeAttr("readonly");
		}
	});
	$("select#updateBuyCountE").change(function(){
		var value = $("select#updateBuyCountE").val();
		if(value == ""){
			$("#updateBuyCountEText").val("");
			$("#updateBuyCountEText").attr("readonly","readonly");
		}else{
			$("#updateBuyCountEText").removeAttr("readonly");
		}
	});
	
	$("#updateWeightType").change(function(){
		var value = $("#updateWeightType").val();
		if(value == "" || value == "SR"){
			$("#updateWeightB")[0].style.display="none";
			$("#updateWeightText")[0].style.display="none";
			$("#updateWeightText").attr("readonly","readonly");
			$("#updateWeightBText")[0].style.display="none";
			$("#updateWeightEText")[0].style.display="none";
			$("#updateWeightB").val("");
			$("#updateWeightText").val("");
			$("#updateWeightBText").val("");
			$("#updateWeightEText").val("");
		}else if(value == "NOTRANGE"){
			$("#updateWeightB")[0].style.display="";
			$("#updateWeightText")[0].style.display="";
			$("#updateWeightBText")[0].style.display="none";
			$("#updateWeightEText")[0].style.display="none";
			$("#updateWeightBText").val("");
			$("#updateWeightEText").val("");
		}else if(value == "RANGE"){
			$("#updateWeightBText")[0].style.display="";
			$("#updateWeightEText")[0].style.display="";
			$("#updateWeightB")[0].style.display="none";
			$("#updateWeightText")[0].style.display="none";
			$("#updateWeightText").attr("readonly","readonly");
			$("#updateWeightB").val("");
			$("#updateWeightText").val("");
		}else{
			$("#updateWeightB")[0].style.display="none";
			$("#updateWeightText")[0].style.display="none";
			$("#updateWeightText").attr("readonly","readonly");
			$("#updateWeightBText")[0].style.display="none";
			$("#updateWeightEText")[0].style.display="none";
			$("#updateWeightB").val("");
			$("#updateWeightText").val("");
			$("#updateWeightBText").val("");
			$("#updateWeightEText").val("");
		}
	});
	$("#updateAmountType").change(function(){
		var value = $("#updateAmountType").val();
		if(value == "" || value == "SR"){
			$("#updateAmountB")[0].style.display="none";
			$("#updateAmountText")[0].style.display="none";
			$("#updateAmountText").attr("readonly","readonly");
			$("#updateAmountBText")[0].style.display="none";
			$("#updateAmountEText")[0].style.display="none";
			$("#updateAmountB").val("");
			$("#updateAmountText").val("");
			$("#updateAmountBText").val("");
			$("#updateAmountEText").val("");
		}else if(value == "NOTRANGE"){
			$("#updateAmountB")[0].style.display="";
			$("#updateAmountText")[0].style.display="";
			$("#updateAmountBText")[0].style.display="none";
			$("#updateAmountEText")[0].style.display="none";
			$("#updateAmountBText").val("");
			$("#updateAmountEText").val("");
		}else if(value == "RANGE"){
			$("#updateAmountBText")[0].style.display="";
			$("#updateAmountEText")[0].style.display="";
			$("#updateAmountB")[0].style.display="none";
			$("#updateAmountText")[0].style.display="none";
			$("#updateAmountText").attr("readonly","readonly");
			$("#updateAmountB").val("");
			$("#updateAmountText").val("");
		}else{
			$("#updateAmountB")[0].style.display="none";
			$("#updateAmountText")[0].style.display="none";
			$("#updateAmountText").attr("readonly","readonly");
			$("#updateAmountBText")[0].style.display="none";
			$("#updateAmountEText")[0].style.display="none";
			$("#updateAmountB").val("");
			$("#updateAmountText").val("");
			$("#updateAmountBText").val("");
			$("#updateAmountEText").val("");
		}
	});
	$("#updateBuyCountType").change(function(){
		var value = $("#updateBuyCountType").val();
		if(value == "" || value == "SR"){
			$("#updateBuyCountB")[0].style.display="none";
			$("#updateBuyCountText")[0].style.display="none";
			$("#updateBuyCountText").attr("readonly","readonly");
			$("#updateBuyCountBText")[0].style.display="none";
			$("#updateBuyCountEText")[0].style.display="none";
			$("#updateBuyCountB").val("");
			$("#updateBuyCountText").val("");
			$("#updateBuyCountBText").val("");
			$("#updateBuyCountEText").val("");
		}else if(value == "NOTRANGE"){
			$("#updateBuyCountB")[0].style.display="";
			$("#updateBuyCountText")[0].style.display="";
			$("#updateBuyCountBText")[0].style.display="none";
			$("#updateBuyCountEText")[0].style.display="none";
			$("#updateBuyCountBText").val("");
			$("#updateBuyCountEText").val("");
		}else if(value == "RANGE"){
			$("#updateBuyCountBText")[0].style.display="";
			$("#updateBuyCountEText")[0].style.display="";
			$("#updateBuyCountB")[0].style.display="none";
			$("#updateBuyCountText")[0].style.display="none";
			$("#updateBuyCountText").attr("readonly","readonly");
			$("#updateBuyCountB").val("");
			$("#updateBuyCountText").val("");
		}else{
			$("#updateBuyCountB")[0].style.display="none";
			$("#updateBuyCountText")[0].style.display="none";
			$("#updateBuyCountText").attr("readonly","readonly");
			$("#updateBuyCountBText")[0].style.display="none";
			$("#updateBuyCountEText")[0].style.display="none";
			$("#updateBuyCountB").val("");
			$("#updateBuyCountText").val("");
			$("#updateBuyCountBText").val("");
			$("#updateBuyCountEText").val("");
		}
	});
})
$(function(){
	$(".ui.dropdown").dropdown();
})
//更新服务数据校验
function updateDistribution(){
	$("#updateMessageList").html("");
	var isOk = true;
	
	var id = $("#distriId").val();
	var distriCarrierCode = $("#updateDistriCarrierCode").val();
	var distriIsPickUp = $("#updateDistriIsPickUp").val();
	var distriTypeCheckeds = [];
	var distriPaymentCheckeds = [];
	var distriPropertiesCheckeds = [];
	
	$('input[name="distriType"]:checked').each(function(){ 
		distriTypeCheckeds.push($(this).val()); 
	});
	$('input[name="distriPayment"]:checked').each(function(){ 
		distriPaymentCheckeds.push($(this).val()); 
	});
	$('input[name="distriProperties"]:checked').each(function(){ 
		distriPropertiesCheckeds.push($(this).val()); 
	});
	
	var distriType = distriTypeCheckeds.toString();
	var distriPayment = distriPaymentCheckeds.toString();
	var distriProperties = distriPropertiesCheckeds.toString();
	
	var distriWeightType = $("#updateWeightType").val();
	var distriAmountType = $("#updateAmountType").val();
	var distriBuyCountType = $("#updateBuyCountType").val();
	var distriStopTime = $("#updateDistriStopTime").val();
	
	var	distriWeightOperator = null;
	var distriWeight = null;
	var distriWeightBegin = null;
	var distriWeightEnd = null;
	
	var	distriBuyCountOperator = null;
	var distriBuyCount = null;
	var distriBuyCountBegin = null;
	var distriBuyCountEnd = null;
	
	var	distriAmountOperator = null;
	var distriAmount = null;
	var distriAmountBegin = null;
	var distriAmountEnd = null;
	
	
	if(distriCarrierCode == "" || distriCarrierCode == null){
		isOk =false;
		$("#messageList").append("<li>配送商必选！</li>");
	}
	if(distriIsPickUp =="" || distriIsPickUp == null){
		isOk = false;
		$("#messageList").append("<li>请选择是否支持上门取件！</li>");
	}
	
	if(distriType.length == 0 || distriType == null){
		$("#messageList").append("<li>请选择配送方式！</li>");
	}
	if(distriPayment.length == 0 || distriPayment == null){
		$("#messageList").append("<li>请选择支付方式！</li>");
	}
	if(distriProperties.length == 0 || distriProperties == null){
		$("#messageList").append("<li>请选择配送属性！</li>");
	}
	
	if(distriWeightType == "" || distriWeightType == null){
		isOk = false;
		$("#messageList").append("<li>请选择重量！</li>");
	}else if(distriWeightType == "RANGE"){
		 var weightBText = $("#updateWeightBText").val();
		 var weightEText = $("#updateWeightEText").val();
		 if(weightBText == "" || weightBText == null || weightEText == "" || weightEText == null || parseFloat(weightBText) > parseFloat(weightEText)){
			 isOk = false;
			 $("#messageList").append("<li>重量区间不能为空或前区间大于后区间!</li>");
		 }else{
			 distriWeightBegin = weightBText;
			 distriWeightEnd = weightEText;
		 }
	}else if(distriWeightType == "NOTRANGE" ){
		var weightB = $("#updateWeightB").val();
		if(weightB == "" || weightB == null){
			isOk = false;
			$("#messageList").append("<li>请选择非区间重量的运算符!</li>");
		}else{
			var weight = $("#updateWeightText").val();
			if(weight == "" || weight == null){
				isOk = false;
				$("#messageList").append("<li>请输入非区间重量的值!</li>");
			}else{
				distriWeightOperator = weightB;
				distriWeight = weight;
			}
		}
	}	
		
	if(distriAmountType == "" || distriAmountType == null){
		isOk = false;
		$("#updateMessageList").append("<li>请选择金额！</li>");
	}else if(distriAmountType == "RANGE"){
		 var amountBText = $("#updateAmountBText").val();
		 var amountEText = $("#updateAmountEText").val();
		 if(amountBText == "" || amountBText == null || amountEText == "" || amountEText == null || parseFloat(amountBText) > parseFloat(amountEText)){
			 isOk = false;
			 $("#updateMessageList").append("<li>金额区间不能为空或前区间大于后区间!</li>");
		 }else{
			 distriAmountBegin = amountBText;
			 distriAmountEnd = amountEText;
		 }
	}else if(distriAmountType == "NOTRANGE" ){
		var amountB = $("#updateAmountB").val();
		if(amountB == "" || amountB == null){
			isOk = false;
			$("#updateMessageList").append("<li>请选择非区间金额的运算符!</li>");
		}else{
			var amount = $("#updateAmountText").val();
			if(amount == "" || amount == null){
				isOk = false;
				$("#updateMessageList").append("<li>请输入非区间金额的值!</li>");
			}else{
				distriAmountOperator = amountB;
				distriAmount = amount;
			}
		}
	}
	
	if(distriBuyCountType == "" || distriBuyCountType == null){
		isOk = false;
		$("#updateMessageList").append("<li>请选择次数！</li>");
	}else if(distriBuyCountType == "RANGE"){
		 var buyCountBText = $("#updateBuyCountBText").val();
		 var buyCountEText = $("#updateBuyCountEText").val();
		 if(buyCountBText == "" || buyCountBText == null || buyCountEText == "" || buyCountEText == null || parseInt(buyCountBText) > parseInt(buyCountEText)){
			 isOk = false;
			 $("#updateMessageList").append("<li>次数区间不能为空或前区间大于后区间!</li>");
		 }else{
			 distriBuyCountBegin = buyCountBText;
			 distriBuyCountEnd =  buyCountEText;
		 }
	}else if(distriBuyCountType == "NOTRANGE" ){
		var buyCountB = $("#updateBuyCountB").val();
		if(buyCountB == "" || buyCountB == null){
			isOk = false;
			$("#updateMessageList").append("<li>请选择非区间次数的运算符!</li>");
		}else{
			var buyCount = $("#updateBuyCountText").val();
			if(buyCount == "" || buyCount == null){
				isOk = false;
				$("#updateMessageList").append("<li>请输入非区间次数的值!</li>");
			}else{
				distriBuyCountOperator = buyCountB;
				distriBuyCount = buyCount;
			}
		}
	}
	
	var strs = distriStopTime.split(":");
	if(strs == "" || strs == null || strs == undefined || strs.length >2 ||  strs.length == 0){
		isOk = false;
		$("#updateMessageList").append("<li>请输入24小时制的小时分钟截单时间,例如:17:00;或截单时间不能为空!</li>");
	}
	
	if(isOk){
		 $("#updateValidationMessage")[0].style.display="none";
		 $.ajax({
	        type: "POST",
	        url: "/distribution/validation",
	        data: {
	        	id,
	        	distriCarrierCode,
	        	distriIsPickUp,
	        	distriType,
	        	distriPayment,
	        	distriProperties,
	        	distriWeightType,
	        	distriAmountType,
	        	distriBuyCountType,
	        	distriStopTime,
	        	distriWeightOperator,
	        	distriWeight,
	        	distriWeightBegin,
	        	distriWeightEnd,
	        	distriBuyCountOperator,
	        	distriBuyCount,
	        	distriBuyCountBegin,
	        	distriBuyCountEnd,
	        	distriAmountOperator,
	        	distriAmount,
	        	distriAmountBegin,
	        	distriAmountEnd
	       		},
	        cache: false,
	        timeout: 600000,
	        success: function (data) {
	        	if(data.status == 500){
	        		$("#updateMessageList").append("<li>数据未发生任何修改!请重新输入服务数据!</li>");
	        		$("#updateValidationMessage")[0].style.display="";
	        	}else if( data.status == 200){
	        		$("#updateForm").submit();
	        	}else{
	        		$("#updateMessageList").append("<li>修改服务数据发生未知错误!请稍后重试!</li>");
	        	}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	alert("error");
	        	alert(jqXHR.responseText);
	        }
	    }); 
	}else{
		$("#updateValidationMessage")[0].style.display="";
	}
	
}
$('.message .close').on('click', function() {
  	$("#updateMessageList").html("");
  	$("#updateValidationMessage")[0].style.display="none";
});
